{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NeighborLoader\n",
    "2.0.0以后逐渐使用NeighborLoader代替NeighborSampler"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ data (torch_geometric.data.Data or torch_geometric.data.HeteroData) – Data或HeteroData对象.\n",
    "+ num_neighbors (List[int] or Dict[Tuple[str, str, str], List[int]]) – 采样的邻居数量，用整型列表传入，列表维度表示采样跳数。每跳采样的邻居相同时可以直接用 **[num_neighbor] * num_hop** 表示\n",
    "\n",
    "+ input_nodes (torch.Tensor or str or Tuple[str, torch.Tensor]) – 采样的起始节点索引，传入训练、验证、测试节点用于划分点集\n",
    "\n",
    "+ replace (bool, optional) – 是否在采样时替换存储 (default: False)\n",
    "+ directed (bool, optional) – 边是否有方向，设为否的化会忽略边的方向，返回所有采样节点之间的边 (default: True)\n",
    "+ transform (Callable, optional) – 转换器 (default: None)\n",
    "+ kwargs (optional) – 其他需要附加的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 重点!\n",
    "在输出中，input_nodes中包含的节点特征和标签会直接放在集合前len(input_nodes)个位置上，不需要原节点索引位置和新节点索引位置的映射关系，在训练集、验证集和测试集上直接计算前几个即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-15T07:37:34.321530Z",
     "start_time": "2021-09-15T07:37:30.227815Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using backend: pytorch\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"2\"\n",
    "\n",
    "from torch_geometric.loader import NeighborLoader\n",
    "from ogb.nodeproppred import PygNodePropPredDataset, Evaluator\n",
    "import torch_geometric.transforms as T\n",
    "from torch_geometric.loader import DataLoader\n",
    "\n",
    "name = 'ogbn-products'\n",
    "\n",
    "dataset = PygNodePropPredDataset(name = name, root = 'dataset/')\n",
    "data = dataset[0]\n",
    "split_idx = dataset.get_idx_split() \n",
    "\n",
    "loader = NeighborLoader(\n",
    "    data,\n",
    "    # Sample 30 neighbors for each node for 2 iterations\n",
    "    num_neighbors=[30] * 2,\n",
    "    # Use a batch size of 128 for sampling training nodes\n",
    "    batch_size=128,\n",
    "    input_nodes=split_idx['train'],\n",
    "    directed = False\n",
    "    )\n",
    "\n",
    "sampled_data = next(iter(loader))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-15T08:20:57.919645Z",
     "start_time": "2021-09-15T08:20:57.912116Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "128\n",
      "Data(edge_index=[2, 2773968], x=[69879, 100], y=[69879, 1], batch_size=128)\n",
      "tensor([[ 3771,  3818,   145,  ..., 69627,  3769, 69380],\n",
      "        [    0,     0,     0,  ..., 69878, 69878, 69878]])\n"
     ]
    }
   ],
   "source": [
    "print(sampled_data.batch_size)\n",
    "print(sampled_data)\n",
    "print(sampled_data.edge_index)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "song",
   "language": "python",
   "name": "song"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
